﻿/*
Kakine
ActionScript Framework
Copyright © 2011, PUNY, LLC.

This file is part of KAKINE.

KAKINE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

KAKINE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with KAKINE.  If not, see <http://www.gnu.org/licenses/>.
*/ 
package com.puny.mvc.views.effect {

	import com.puny.mvc.views.effect.events.ParticleEvent;
	
	import flash.display.*;
	import flash.geom.Point;
	import flash.utils.getTimer;
	
	/**
	 * @private
	 * Particle class
	 * depuricated
	 */
	public class Particle { 
		protected var _vx:	Number = 0; 		// the x and y velocity of the particle
		protected var _vy:	Number = 0;
		protected var _fy:Number = 9.8*100;		// the x and y force of the particle
		protected var _fx:Number = 0;
		protected var _count:Number = 0;
		protected var _clip:Sprite;				// display object for the particle
		
		private var _last:int = -1;
		private var _initX:Number;
		private var _initY:Number;
		private var _initVx:Number;
		private var _initVy:Number;
		private var _moving:Boolean = true;
		private var _event:ParticleEvent = null;
		
		/**
		 * Constructor of Particle Class
		 * @param spriteclass visual asset for this particle
		 * @param container container for this particle
		 * @param px initial x position
		 * @param py initial y position. Positive is higher position than negative.
		 */
		public function Particle ( spriteclass : Sprite, container : Sprite, px : Number, py : Number)	{
			
			// instantiate a new particle graphic
			_clip = spriteclass;
			
			// and add it to the stage
			container.addChild(_clip); 
			_clip.visible = false;
			
			initializePosition(px, py);	
		}
		
		/**
		 * Set initial position
		 * @param px initial x position
		 * @param py initial y position. Positive is higher position than Negative.
		 */
		public function initializePosition(px:Number, py:Number):void {
			_initX = px;
			_initY = py;
		}
	
		/**
		 * Rundom number generator between min and max.
		 * min and max are switchable.
		 * @param min minimum number
		 * @param max maximum number
		 */
		protected function randRange(min:Number, max:Number):Number	{
		    var randomNum:Number = (Math.random() * (max - min )) + min;
		    return randomNum;
		}
		
		/**
		 * Set initial velocity of the particle
		 * @param vx x velocity. Positive is going right. Negative is going left.
		 * @param vy y velocity. Positive is going up. Negative is going down.
		 */
		public function setVel(vx:Number, vy:Number):void {
			_vx = vx;
			_vy = vy;
			_initVx = vx;
			_initVy = vy;
		}
		
		/**
		 * Define special effects when the position is updated.
		 */
		protected function specialUpdate():void {}
		
		/**
		 * Define special effects when the particle is initialized.
		 */
		protected function specialInitialize():void {}
	
		/**
		 * Define conditions when the particle should be initialized.
		 * @return true if the particle should be initialized.
		 */
		protected function isOver():Boolean {
			return _clip.y > 0;
		}
		
		private function initialize():void {
			_clip.x = _initX;
			_clip.y = -_initY;
			_clip.visible = true;
			_vx = _initVx;
			_vy = _initVy;
			_count = 0;
			specialInitialize();
		}
		
		/**
		 * Make the particle moveable.
		 */
		public function moveable():void {
			_moving = true;
		}
		
		/**
		 * Make the particle stop.
		 */
		public function stop():void {
			_moving =false;
		}
		
		/**
		 * event getter
		 * @return an event
		 */
		public function get event():ParticleEvent {
			return _event;
		}
		
		/**
		 * update the particle position
		 * @return true if the particle is still moving.
		 */
		final public function update():Boolean {
			var now:int = getTimer();
			
			if (_last == -1&&_moving) {
				_event = null;
				initialize();
			} else if (_last == -1) {
				_event = null;
				return false;
			} else if (isOver()) {
				_event = new ParticleEvent(ParticleEvent.END_OF_CYCLE);
				_event.position = new Point(_clip.x,_clip.y);
				_event.velocity = new Point(_vx,_vy);
			
				_clip.visible = false;
				_last = -1;

				return false;
			} else {
				var dt:Number = (now - _last)/1000;
				var dt2:Number = dt * dt;
				// add the velocity to the particle's position... 
				_clip.x += _vx*dt + .5 * _fx * dt2;
				_clip.y += -(_vy*dt +  .5 * _fy * dt2);

				_vx += _fx*dt;
				_vy += _fy*dt;
				_count++;
				specialUpdate();
			}
			
			_last = now;
			return true;
		}
		
	}
}